package com.example.jeong.yuejian10.Activity;



import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jeong.yuejian10.Bean.EnterMatchBean;
import com.example.jeong.yuejian10.Bean.MatchBean;
import com.example.jeong.yuejian10.Bean.UserBean;
import com.example.jeong.yuejian10.Model.MatchModel;
import com.example.jeong.yuejian10.Model.UserModel;
import com.example.jeong.yuejian10.R;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

//比赛报名页
public class EnterMatchActivity extends Activity {

    private ImageView left;
    private Button enterbutton;
    private int mId,uid;
    private TextView matchname,matchprice,username,userphone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enter_match);
        mId=getIntent().getIntExtra("m_id",-1);
        // Log.d("m","mId");
        initViews();//初始化
        initEvents();//点击事件处理
    }
    private void initViews(){
        left = (ImageView) findViewById(R.id.left_match);
        enterbutton = (Button) findViewById(R.id.entermatch_button);
        matchname = (TextView) findViewById(R.id.m_name);
        matchprice = (TextView) findViewById(R.id.m_price);
        username = (TextView) findViewById(R.id.u_name);
        userphone = (TextView) findViewById(R.id.u_phone);
    }
    private void initEvents(){
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        SharedPreferences sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        uid = sp.getInt("u_id", 0);

        UserModel userModel = new UserModel();
        Call<UserBean> callu = userModel.UserInfo(uid);
        callu.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                UserBean userBean = response.body();
                username.setText(userBean.getU_name());
                userphone.setText(userBean.getU_phone());
            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {

            }
        });

        MatchModel matchModel = new MatchModel();
        Call<MatchBean> callm = matchModel.getMatchDetail(mId);
        callm.enqueue(new Callback<MatchBean>() {
            @Override
            public void onResponse(Call<MatchBean> call, Response<MatchBean> response) {
                MatchBean data = response.body();//获取数据
                String mname = data.getM_name();

                int mprice = data.getM_price();
                matchname.setText(mname);
                //   Log.d("m","matchname");
                matchprice.setText("￥"+mprice+"");
                // Log.d("m","matchprice");
            }

            @Override
            public void onFailure(Call<MatchBean> call, Throwable t) {

            }
        });
        enterbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MatchModel matchModel = new MatchModel();
                Call<EnterMatchBean> call = matchModel.addMatch(uid,mId);
                call.enqueue(new Callback<EnterMatchBean>() {
                    @Override
                    public void onResponse(Call<EnterMatchBean> call, Response<EnterMatchBean> response) {
                        EnterMatchBean matchBean = response.body();
                        if ("1".equals(matchBean.getSuccess())) {
                            Toast.makeText(EnterMatchActivity.this, "报名成功", Toast.LENGTH_LONG).show();

                        }else if("2".equals(matchBean.getSuccess())){
                            Toast.makeText(EnterMatchActivity.this, "请不要重复报名", Toast.LENGTH_LONG).show();
                        }
                        else {
                            Toast.makeText(EnterMatchActivity.this, "失败", Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<EnterMatchBean> call, Throwable t) {

                    }
                });
            }
        });

    }
}
